/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unesp.demac.bcc.soo.dao;

import br.unesp.demac.bcc.soo.bean.Login;
import br.unesp.demac.bcc.soo.bean.Pessoa;
import br.unesp.demac.bcc.soo.util.FabricaConexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author flavioam
 */
public class LoginDAOImp implements LoginDAO {

    @Override
    public Login selecionaLoginByIdPessoa(int idPessoa) {

        Login l = null;
        Connection con = null;

        try {

            con = FabricaConexao.getConexao();

            String sql = "select * from Logins where Pessoas_idPessoas = ?";

            PreparedStatement pstmt = con.prepareStatement(sql);

            pstmt.setInt(1, idPessoa);

            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {

                l = new Login();
                l.setSenha(rs.getString("senha"));
                l.setUsuario(rs.getString("usuario"));
            }

        } catch (SQLException e) {

            e.printStackTrace();
            throw new Exception(e.getMessage());

        } catch (Exception e) {

            e.printStackTrace();
            throw new Exception(e.getMessage());

        } finally {

            FabricaConexao.fechar(con);
            return l;
        }
    }

    @Override
    public boolean salvaLogin(Pessoa p,Connection con) {

        boolean saved = false;

        try {

            String sql = "insert into Logins (usuario, senha,Pessoas_idPessoas) values (?,?,?)";

            PreparedStatement pstmt = con.prepareStatement(sql);
            
            Login l = p.getLogin();

            pstmt.setString(1, l.getUsuario());
            pstmt.setString(2, l.getSenha());
            pstmt.setInt(3, p.getIdPessoa());

            pstmt.executeUpdate();

            saved = true;

        } catch (SQLException e) {

           throw new Exception(e.getMessage());

        } catch (Exception e) {

            throw new Exception(e.getMessage());

        } finally {
            return saved;
        }
    }

    @Override
    public boolean autenticacao(Login l) {

        boolean autenticated = false;
        Connection con = null;

        try {

            con = FabricaConexao.getConexao();

            String sql = "select * from Logins where usuario = ?";

            PreparedStatement pstmt = con.prepareStatement(sql);

            pstmt.setString(1, l.getUsuario());

            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {

                String senha = rs.getString("senha");

                if (senha.equals(l.getSenha())) {

                    autenticated = true;
                }
            }

        } catch (SQLException e) {

            throw new Exception(e.getMessage());

        } catch (Exception e) {

            throw new Exception(e.getMessage());

        } finally {
            FabricaConexao.fechar(con);
            return autenticated;
        }
    }
    
    @Override
    public boolean editaLogin(Pessoa p,Connection con) {

        boolean saved = false;

        try {

            String sql = "update Logins set usuario = ?, senha = ? where Pessoas_idPessoas = ?";
 System.out.println(sql);
            PreparedStatement pstmt = con.prepareStatement(sql);
            
            Login l = p.getLogin();

            pstmt.setString(1, l.getUsuario());
            pstmt.setString(2, l.getSenha());
            pstmt.setInt(3, p.getIdPessoa());

            pstmt.executeUpdate();

            saved = true;

        } catch (SQLException e) {

            e.printStackTrace();
           throw new Exception(e.getMessage());

        } catch (Exception e) {

            e.printStackTrace();
            throw new Exception(e.getMessage());

        } finally {
            
            System.out.println(saved);
            return saved;
        }
    }
}
